//
//  UIViewController+RSSystemUtit.m
//  RSTools
//
//  Created by thinkjoy on 2017/9/14.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "UIViewController+RSSystemUtit.h"

//发短信|邮件使用
#import <MessageUI/MessageUI.h>

#pragma mark - 日志开关
#ifdef DEBUG
#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define DLog(...)
#endif

@implementation UIViewController (RSSystemUtit)

#pragma mark    - 在APP内发送短信，发送或取消后退出编辑短信界面
- (void )sendSMS:(NSString *)bodyOfMessage recipients:(NSArray *)recipients
{
    MFMessageComposeViewController *megVC = [[MFMessageComposeViewController alloc] init];
    if([MFMessageComposeViewController canSendText]) {//必须添加判断，防止在不支持发送短信的设备上崩溃
        
        megVC.body = bodyOfMessage;
        if ([recipients count]) {
            megVC.recipients = recipients;
        }
        
        megVC.messageComposeDelegate = (id)self;
        [self presentViewController:megVC animated:YES completion:^{ }];
    }else{
        //该方法中，设备不支持有相关提示，不需要在添加提示语
        //        UIAlertController * alertC  =   [UIAlertController alertControllerWithTitle:@"提示" message:@"当前设备不支持发送短信" preferredStyle:UIAlertControllerStyleAlert];
        //        UIAlertAction * action  =   [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:nil];
        //        [alertC addAction:action];
        //        [self presentViewController:alertC animated:YES completion:nil];
    }
}
//发送|取消短信后退回本页面
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
    [self dismissViewControllerAnimated:YES completion:nil];
    DLog(@"result=%d",(int)result);
    switch (result) {
        case MessageComposeResultCancelled:
        {
            DLog(@"取消发送");
        }
            break;
        case MessageComposeResultSent:
        {
            DLog(@"发送成功");
        }
            break;
        case MessageComposeResultFailed:
        {
            DLog(@"发送失败");
        }
            break;
        default:
            break;
    }
}

#pragma mark    - 在APP内发邮件
- (void)sendE_MailWithRecipients:(NSArray <NSString*>*)recipients ccRecipients:(NSArray <NSString*>*)ccRecipients bccRecipients:(NSArray <NSString*>*)bccRecipients subject:(NSString *)subject body:(NSString *)body attachmentDatas:(NSArray <NSDictionary *>*)attachmentDatas
{
    MFMailComposeViewController * mailVC    =   [[MFMailComposeViewController alloc] init];
    
    if ([MFMailComposeViewController canSendMail]) {//必须添加判断，防止在不支持发送邮件的设备上崩溃
        //添加收件人
        [mailVC setToRecipients: recipients];
        
        //添加抄送
        [mailVC setCcRecipients:ccRecipients];
        
        //添加密送
        [mailVC setBccRecipients:bccRecipients];
        
        //设置主题
        [mailVC setSubject: subject];
        
        //添加邮件内容
        [mailVC setMessageBody:body isHTML:NO];
        
        //添加附件
        if ([attachmentDatas count]) {
            for (NSDictionary * dataDic in attachmentDatas) {
                NSData * data   =   dataDic[RSSendE_MailAttachmentData];
                NSString * mimeType =   dataDic[RSSendE_MailMimeType];
                NSString * fileName =   dataDic[RSSendE_MailFilename];
                
                mimeType    =   mimeType.length>0?mimeType:@"";
                fileName    =   fileName.length>0?fileName:@"";
                if (data) {
                    [mailVC addAttachmentData:data mimeType:mimeType fileName:fileName];
                }
            }
        }
        
        mailVC.mailComposeDelegate = (id)self;
        
        [self presentViewController:mailVC animated:YES completion:nil];
    }
}
//发送|保存|取消邮件后退回本页面
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    //关闭邮件发送窗口
    [self dismissViewControllerAnimated:controller completion:nil];
    DLog(@"result=%d",(int)result);
    
    NSString *msg;
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"用户取消编辑邮件";
            break;
        case MFMailComposeResultSaved:
            msg = @"用户成功保存邮件";
            break;
        case MFMailComposeResultSent:
            msg = @"用户点击发送，将邮件放到队列中，还没发送";
            break;
        case MFMailComposeResultFailed:
            msg = @"用户试图保存或者发送邮件失败";
            break;
        default:
            msg = @"";
            break;
    }
}


@end
